package com.topone.controller;

import com.topone.service.FileLocalStorageService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/file/image")
public class ImageController {

    @Autowired
    private FileLocalStorageService fileLocalStorageService;

    @RequestMapping(value = "/{imageId}",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public byte[] readImageThumbnails(@PathVariable Integer imageId) throws IOException {
        try (InputStream in = fileLocalStorageService.loadImage(imageId).getDataInput()) {
            return IOUtils.toByteArray(in);
        } catch (IOException e) {
            throw e;
        }
    }
}
